package com.neouton.crm.pm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.neouton.common.entity.ActivitiVO;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.Constants;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.approval.entity.ApprovalVO;
import com.neouton.crm.approval.service.impl.ApprovalServiceImpl;
import com.neouton.crm.entity.FileUploadRecordVO;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.op.entity.SalesOpportunityVO;
import com.neouton.crm.op.service.SalesOpportunityService;
import com.neouton.crm.paymentPlan.entity.PaymentPlan;
import com.neouton.crm.pm.entity.ProjectUpdateInfoVO;
import com.neouton.crm.pm.entity.PurchaseOrderVO;
import com.neouton.crm.pm.entity.SalesProjectManageVO;
import com.neouton.crm.pm.service.ProjectMemberService;
import com.neouton.crm.pm.service.SalesProjectManageService;
import com.neouton.crm.sales.entity.*;
import com.neouton.crm.sales.service.SalesFileRelationService;
import com.neouton.crm.supplier.entity.SupplierVO;
import com.neouton.feginClient.FileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author TheSail
 * @date 2022/11/04 13:04:40
 * @description
 */
@RequestMapping("projectmanage")
@RestController
public class ProjectManageController {

    @Resource
    private SalesProjectManageService projectManageService;
    @Resource
    private ProjectMemberService projectMemberService;
    @Resource
    private SalesOpportunityService salesOpportunityService;
    @Resource
    private FileService fileService;
    @Resource
    private SalesFileRelationService salesFileRelationService;
    @Resource
    private ApprovalServiceImpl approvalService;

    /**
     * 根据项目id 获取全部回款计划
     *
     * @param id
     * @return
     */
    @GetMapping("/getProjectAllBackmoneyPlan/{id}")
    public ResponseAPI getProjectAllBackmoneyPlan(@PathVariable Integer id) {
        if (id == null) {
            return ResponseAPI.error("系统异常，请联系管理员");
        }
        return ResponseAPI.successData(projectManageService.getProjectAllBackmoneyPlan(id));
    }

    /**
     * 根据客户id 获取全部回款计划
     *
     * @param customerId
     * @return
     */
    @GetMapping("/getProjectAllBackmoneyPlanByCustomerId/{customerId}")
    public ResponseAPI getProjectAllBackmoneyPlanByCustomerId(@PathVariable Integer customerId) {
        if (customerId == null) {
            return ResponseAPI.error("系统异常，请联系管理员");
        }
        return ResponseAPI.successData(projectManageService.getProjectAllBackmoneyPlanByCustomerId(customerId));
    }

    /**
     * 查询项目
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryProject/{pageNum}/{pageSize}")
    public ResponseAPI queryProject(@PathVariable(value = "pageNum") Integer pageNum,
                                    @PathVariable(value = "pageSize") Integer pageSize,
                                    @RequestBody SalesProjectManageVO projectManageVO,
                                    @RequestHeader("token") String token) {
//        Integer userId = AuthUtils.getUserId();
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(), Constants.OPPORTUNITY_MODEL_NAME, token);
//        int authId = projectMemberService.queryAuth(userId,Constants.PROJECT_MODEL_NAME);
        IPage<SalesProjectManageVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<SalesProjectManageVO> list = projectManageService.queryProject(page, projectManageVO, userList, token);
//        list.setTotal(total);
        return ResponseAPI.successData(list);
    }

    /**
     * 查询项目
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryUpdateProject/{pageNum}/{pageSize}")
    public ResponseAPI queryUpdateProject(@PathVariable(value = "pageNum") Integer pageNum,
                                    @PathVariable(value = "pageSize") Integer pageSize,
                                    @RequestBody SalesProjectManageVO projectManageVO,
                                    @RequestHeader("token") String token) {
//        Integer userId = AuthUtils.getUserId();
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(), Constants.OPPORTUNITY_MODEL_NAME, token);
//        int authId = projectMemberService.queryAuth(userId,Constants.PROJECT_MODEL_NAME);
        IPage<SalesProjectManageVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<SalesProjectManageVO> list = projectManageService.queryUpdateProject(page, projectManageVO, userList, token);
//        list.setTotal(total);
        return ResponseAPI.successData(list);
    }

    /**
     * 查询项目
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryProjectList")
    public ResponseAPI queryProjectList(@RequestBody SalesProjectManageVO projectManageVO,
                                    @RequestHeader("token") String token) {
        List<SysUserEntity> userList = projectMemberService.queryUserProjectList(AuthUtils.getUserId(), token);
        List<SalesProjectManageVO> list = projectManageService.queryProject( projectManageVO, userList, token);
        return ResponseAPI.successData(list);
    }

    /**
     * 查询项目     待办  初验
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryProjectOneToDo/{pageNum}/{pageSize}")
    public ResponseAPI queryProjectOneToDo(@PathVariable(value = "pageNum") Integer pageNum,
                                        @PathVariable(value = "pageSize") Integer pageSize,
                                        @RequestBody SalesProjectManageVO projectManageVO,
                                        @RequestHeader("token") String token) {
        ApprovalVO approvalVO = new ApprovalVO();
        approvalVO.setApprovalType(Constants.PROJECT_TASK_TYPE_ONE);
        List<ApprovalVO> appList = approvalService.queryApprovalByPage(approvalVO);
        if (appList == null || appList.isEmpty()) {
            return ResponseAPI.successData(new PageUtil(new PageDTO()));
        } else {
            ArrayList<Integer> arrayList = new ArrayList<>();
            for (ApprovalVO item : appList) {
                arrayList.add(item.getApprovalId());
            }
            projectManageVO.setApprovalOneList(arrayList);
            IPage<SalesProjectManageVO> page = new Page<>(pageNum, pageSize, true);
            PageUtil<SalesProjectManageVO> list = projectManageService.queryProjectAudit(page, projectManageVO, null, token);
            List<SalesProjectManageVO> records = (List<SalesProjectManageVO>) list.getRecords();
            for (SalesProjectManageVO item:records) {
                item.setApplicant(item.getApplicantOne());
                item.setApplyTime(item.getApplyTimeOne());
                item.setNextApprover(item.getNextApproverOne());
                item.setApprovalCode(item.getApprovalCodeOne());
                item.setReasonApplication(item.getReasonApplicationOne());
            }
            list.setRecords(records);
            return ResponseAPI.successData(list);
        }
    }

    /**
     * 查询项目     已办  初验
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryProjectOneDone/{pageNum}/{pageSize}")
    public ResponseAPI queryProjectOneDone(@PathVariable(value = "pageNum") Integer pageNum,
                                        @PathVariable(value = "pageSize") Integer pageSize,
                                        @RequestBody SalesProjectManageVO projectManageVO,
                                        @RequestHeader("token") String token) {
        ApprovalVO approvalVO = new ApprovalVO();
        approvalVO.setApprovalType(Constants.PROJECT_TASK_TYPE_ONE);
        List<ApprovalVO> appList = approvalService.queryApprovalDoneRecordByType(approvalVO);
        if (appList == null || appList.isEmpty()) {
            return ResponseAPI.successData(new PageUtil(new PageDTO()));
        } else {
            ArrayList<Integer> arrayList = new ArrayList<>();
            for (ApprovalVO item : appList) {
                arrayList.add(item.getApprovalId());
            }
            projectManageVO.setApprovalOneList(arrayList);
            IPage<SalesProjectManageVO> page = new Page<>(pageNum, pageSize, true);
            PageUtil<SalesProjectManageVO> list = projectManageService.queryProjectAudit(page, projectManageVO, null, token);
            List<SalesProjectManageVO> records = (List<SalesProjectManageVO>) list.getRecords();
            for (SalesProjectManageVO item:records) {
                item.setApplicant(item.getApplicantOne());
                item.setApplyTime(item.getApplyTimeOne());
                item.setNextApprover(item.getNextApproverOne());
                item.setApprovalCode(item.getApprovalCodeOne());
                item.setReasonApplication(item.getReasonApplicationOne());
            }
            list.setRecords(records);
            return ResponseAPI.successData(list);
        }
    }

    /**
     * 查询项目     记录  初验
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryProjectOneRecord/{pageNum}/{pageSize}")
    public ResponseAPI queryProjectOneRecord(@PathVariable(value = "pageNum") Integer pageNum,
                                          @PathVariable(value = "pageSize") Integer pageSize,
                                          @RequestBody SalesProjectManageVO projectManageVO,
                                          @RequestHeader("token") String token) {
        ApprovalVO approvalVO = new ApprovalVO();
        approvalVO.setApprovalType(Constants.PROJECT_TASK_TYPE_ONE);
        List<ApprovalVO> appList = approvalService.queryApprovalRecordByType(approvalVO,token);
        if (appList == null || appList.isEmpty()) {
            return ResponseAPI.successData(new PageUtil(new PageDTO()));
        } else {
            ArrayList<Integer> arrayList = new ArrayList<>();
            for (ApprovalVO item : appList) {
                arrayList.add(item.getApprovalId());
            }
            projectManageVO.setApprovalOneList(arrayList);
            IPage<SalesProjectManageVO> page = new Page<>(pageNum, pageSize, true);
            PageUtil<SalesProjectManageVO> list = projectManageService.queryProjectAudit(page, projectManageVO, null, token);
            List<SalesProjectManageVO> records = (List<SalesProjectManageVO>) list.getRecords();
            for (SalesProjectManageVO item:records) {
                item.setApplicant(item.getApplicantOne());
                item.setApplyTime(item.getApplyTimeOne());
                item.setNextApprover(item.getNextApproverOne());
                item.setApprovalCode(item.getApprovalCodeOne());
                item.setReasonApplication(item.getReasonApplicationOne());
            }
            list.setRecords(records);
            return ResponseAPI.successData(list);
        }
    }

    /**
     * 查询项目     待办
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryProjectToDo/{pageNum}/{pageSize}")
    public ResponseAPI queryProjectToDo(@PathVariable(value = "pageNum") Integer pageNum,
                                    @PathVariable(value = "pageSize") Integer pageSize,
                                    @RequestBody SalesProjectManageVO projectManageVO,
                                    @RequestHeader("token") String token) {
        ApprovalVO approvalVO = new ApprovalVO();
        approvalVO.setApprovalType(Constants.PROJECT_TASK_TYPE);
        List<ApprovalVO> appList = approvalService.queryApprovalByPage(approvalVO);
        if (appList == null || appList.isEmpty()) {
            return ResponseAPI.successData(new PageUtil(new PageDTO()));
        } else {
            ArrayList<Integer> arrayList = new ArrayList<>();
            for (ApprovalVO item : appList) {
                arrayList.add(item.getApprovalId());
            }
            projectManageVO.setApprovalList(arrayList);
            IPage<SalesProjectManageVO> page = new Page<>(pageNum, pageSize, true);
            PageUtil<SalesProjectManageVO> list = projectManageService.queryProjectAudit(page, projectManageVO, null, token);
            return ResponseAPI.successData(list);
        }
    }

    /**
     * 查询项目     已办
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryProjectDone/{pageNum}/{pageSize}")
    public ResponseAPI queryProjectDone(@PathVariable(value = "pageNum") Integer pageNum,
                                        @PathVariable(value = "pageSize") Integer pageSize,
                                        @RequestBody SalesProjectManageVO projectManageVO,
                                        @RequestHeader("token") String token) {
        ApprovalVO approvalVO = new ApprovalVO();
        approvalVO.setApprovalType(Constants.PROJECT_TASK_TYPE);
        List<ApprovalVO> appList = approvalService.queryApprovalDoneRecordByType(approvalVO);
        if (appList == null || appList.isEmpty()) {
            return ResponseAPI.successData(new PageUtil(new PageDTO()));
        } else {
            ArrayList<Integer> arrayList = new ArrayList<>();
            for (ApprovalVO item : appList) {
                arrayList.add(item.getApprovalId());
            }
            projectManageVO.setApprovalList(arrayList);
            IPage<SalesProjectManageVO> page = new Page<>(pageNum, pageSize, true);
            PageUtil<SalesProjectManageVO> list = projectManageService.queryProjectAudit(page, projectManageVO, null, token);
            return ResponseAPI.successData(list);
        }
    }

    /**
     * 查询项目     记录
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryProjectRecord/{pageNum}/{pageSize}")
    public ResponseAPI queryProjectRecord(@PathVariable(value = "pageNum") Integer pageNum,
                                        @PathVariable(value = "pageSize") Integer pageSize,
                                        @RequestBody SalesProjectManageVO projectManageVO,
                                        @RequestHeader("token") String token) {
        ApprovalVO approvalVO = new ApprovalVO();
        approvalVO.setApprovalType(Constants.PROJECT_TASK_TYPE);
        List<ApprovalVO> appList = approvalService.queryApprovalRecordByType(approvalVO,token);
        if (appList == null || appList.isEmpty()) {
            return ResponseAPI.successData(new PageUtil(new PageDTO()));
        } else {
            ArrayList<Integer> arrayList = new ArrayList<>();
            for (ApprovalVO item : appList) {
                arrayList.add(item.getApprovalId());
            }
            projectManageVO.setApprovalList(arrayList);
            IPage<SalesProjectManageVO> page = new Page<>(pageNum, pageSize, true);
            PageUtil<SalesProjectManageVO> list = projectManageService.queryProjectAudit(page, projectManageVO, null, token);
            return ResponseAPI.successData(list);
        }
    }

    /**
     * 查询项目信息
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryProjectInfo")
    public ResponseAPI queryProjectInfo(@RequestBody SalesProjectManageVO projectManageVO, @RequestHeader("token") String token) {
//        Integer userId = AuthUtils.getUserId();
//        int authId = projectMemberService.queryAuth(userId,Constants.PROJECT_MODEL_NAME);
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(), "projectManage", token);
        List<SalesProjectManageVO> list = projectManageService.queryProject(projectManageVO, userList, token);
        return ResponseAPI.successData(list);
    }

    /**
     * 查询项目信息
     *
     * @param oppId 机会ID
     * @return
     */

    @PostMapping("/queryProjectInfoByOppId/{oppId}")
    public ResponseAPI queryProjectInfoByOppId( @PathVariable(value = "oppId") Integer oppId ) {
        SalesProjectManageVO projectManageVO = projectManageService.queryProjectInfoByOppId(oppId);
        return ResponseAPI.successData(projectManageVO);
    }


    /**
     * 根据id查询项目
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryProjectById/")
    public ResponseAPI queryProjectById(@RequestBody SalesProjectManageVO projectManageVO, @RequestHeader("token") String token) {
        SalesProjectManageVO result = projectManageService.queryProjectInfoById(projectManageVO.getProjectManageId(),token);
        return ResponseAPI.successData(result);
    }

    /**
     * 添加项目
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/addProject/")
    public ResponseAPI addProject(@RequestBody SalesProjectManageVO projectManageVO, @RequestHeader("token") String token) {
        //ADD LiBingkun 修改了项目与机会绑定形式，此代码基本无用
//        List<Integer> opportunity = projectManageService.queryProjectOfOpportunity();
//        if (opportunity.contains(projectManageVO.getProjectOpportunityId())) {
//            return ResponseAPI.error("机会已被占用，请重新选择");
//        }
        projectManageService.addProject(projectManageVO, token);
        return ResponseAPI.successData(projectManageVO.getProjectManageId());
    }

    /**
     * 修改项目
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/updateProject/")
    public ResponseAPI updateProject(@RequestBody SalesProjectManageVO projectManageVO, @RequestHeader("token") String token) {
        Integer opportunityId = projectManageService.queryProjectInfoById(projectManageVO.getProjectManageId(),token).getProjectOpportunityId();
        if (Objects.equals(opportunityId, projectManageVO.getProjectOpportunityId())) {
            int i = projectManageService.updateProject(projectManageVO, token);
            return ResponseAPI.successData(i == -1 ? -1 : projectManageVO.getProjectManageId());
        }
        List<Integer> opportunity = projectManageService.queryProjectOfOpportunity();
        if (opportunity.contains(projectManageVO.getProjectOpportunityId())) {
            return ResponseAPI.error("机会已被占用，请重新选择");
        }
        int i = projectManageService.updateProject(projectManageVO, token);
        return ResponseAPI.successData(i == -1 ? -1 : projectManageVO.getProjectManageId());
    }

    /**
     * 修改项目
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/updateProjectAfterPass/")
    public ResponseAPI updateProjectAfterPass(@RequestBody SalesProjectManageVO projectManageVO, @RequestHeader("token") String token) {
        projectManageService.updateProjectAfterPass(projectManageVO,token);
        return  ResponseAPI.successData("success");
    }




    /**
     * 根据项目ID获取文件ID集合
     *
     * @param projectManageId
     * @return
     */
    @PostMapping("queryFileInfoList/{projectManageId}")
    public ResponseAPI queryFileInfoList(@PathVariable(value = "projectManageId") Integer projectManageId) {
        List<String> result = projectManageService.queryFileInfoList(projectManageId);
        return ResponseAPI.successData(result);
    }

    /**
     * 保存项目与文件关系
     */
    @PostMapping("addProjectFileRelation/{projectManageId}")
    public ResponseAPI addProjectFileRelation(@PathVariable(value = "projectManageId") Integer projectManageId, @RequestBody List<FileUploadRecordVO> fileIds) {
        projectManageService.addProjectFileRelation(projectManageId, fileIds);
        return ResponseAPI.successData("sucess");
    }

    /**
     * 附件逻辑删除、更新人为删除人，更新时间为删除时间
     *
     * @param fileId 需要删除的文件ID
     * @param token
     * @return boolean 是否删除成功
     */
    @PostMapping("/deleteFile/{fileId}")
    public ResponseAPI deleteFile(@PathVariable("fileId") String fileId,
                                  @RequestHeader("token") String token) {
        return ResponseAPI.successData(salesFileRelationService.deleteFileInfo(fileId, Constants.PROJECT_MODEL_ID));
    }

    /**
     * 查询变更信息
     *
     * @param projectManageId
     * @return
     */
    @PostMapping("queryChangeInfo/{projectManageId}")
    public ResponseAPI queryChangeInfo(@PathVariable(value = "projectManageId") Integer projectManageId) {
        List<SalesChangeVO> result = projectManageService.queryChangeInfo(projectManageId);
        return ResponseAPI.successData(result);
    }

    /**
     * 查询跟进记录
     *
     * @param projectManageId
     * @return
     */
    @PostMapping("queryFollowRecord/{projectManageId}")
    public ResponseAPI queryFollowRecord(@PathVariable(value = "projectManageId") Integer projectManageId) {
        List<SalesFollowRecordVO> result = projectManageService.queryFollowRecord(projectManageId);
        return ResponseAPI.successData(result);
    }

    /**
     * 添加跟进记录
     *
     * @param salesFollowRecordVO
     * @return
     */
    @PostMapping("/addFollowRecord/")
    public ResponseAPI addFollowRecord(@RequestBody SalesFollowRecordVO salesFollowRecordVO) {
        return ResponseAPI.successData(projectManageService.addFollowRecord(salesFollowRecordVO));
    }

    /**
     * 修改跟进记录
     *
     * @param salesFollowRecordVO
     * @return
     */
    @PostMapping("/updateFollowRecord/")
    public ResponseAPI updateFollowRecord(@RequestBody SalesFollowRecordVO salesFollowRecordVO) {
        return ResponseAPI.successData(projectManageService.updateFollowRecord(salesFollowRecordVO));
    }

    /**
     * 删除项目跟进记录
     *
     * @param recordId 记录ID
     * @return boolean 删除是否成功
     */
    @DeleteMapping("/deleteFollowRecord/{recordId}")
    public ResponseAPI deleteFollowRecords(@PathVariable("recordId") Integer recordId) {
        return ResponseAPI.successData(projectManageService.deleteFollowRecords(recordId));
    }

    /**
     * 阶段推进
     *
     * @param salesStageVO 推进到的状态对象
     * @return boolean 阶段推进是否成功
     */
    @PostMapping("/changeStage")
    public ResponseAPI changeStage(@RequestBody SalesStageVO salesStageVO) {
        return ResponseAPI.successData(projectManageService.changeStage(salesStageVO));
    }

    /**
     * 查询阶段信息
     *
     * @param projectManageId
     * @return
     */
    @PostMapping("queryStageInfo/{projectManageId}")
    public ResponseAPI queryStageInfo(@PathVariable(value = "projectManageId") Integer projectManageId) {
        List<SalesStageVO> result = projectManageService.queryStageInfo(projectManageId);
        return ResponseAPI.successData(result);
    }

    /**
     * 添加阶段信息
     *
     * @param salesStageVO
     * @return
     */
    @PostMapping("/addStageInfo/")
    public ResponseAPI addStageInfo(@RequestBody SalesStageVO salesStageVO) {
        int i = projectManageService.addStageInfo(salesStageVO);
        return ResponseAPI.successData(i);
    }

    /**
     * 修改阶段信息
     *
     * @param salesStageVO
     * @return
     */
    @PostMapping("/updateStageInfo/")
    public ResponseAPI updateStageInfo(@RequestBody SalesStageVO salesStageVO) {
        int i = projectManageService.updateStageInfo(salesStageVO);
        return ResponseAPI.successData(i);
    }

    /**
     * 查询审核信息
     *
     * @param projectManageId
     * @return
     */
    @PostMapping("queryAuditInfo/{projectManageId}")
    public ResponseAPI queryAuditInfo(@PathVariable(value = "projectManageId") Integer projectManageId) {
        List<SalesAuditVO> result = projectManageService.queryAuditInfo(projectManageId);
        return ResponseAPI.successData(result);
    }

    /**
     * 添加审核信息
     *
     * @param salesAuditVO
     * @return
     */
    @PostMapping("/addAuditInfo/")
    public ResponseAPI addAuditInfo(@RequestBody SalesAuditVO salesAuditVO) {
        int i = projectManageService.addAuditInfo(salesAuditVO);
        return ResponseAPI.successData(i);
    }

    /**
     * 修改审核信息
     *
     * @param salesAuditVO
     * @return
     */
    @PostMapping("/updateAuditInfo/")
    public ResponseAPI updateAuditInfo(@RequestBody SalesAuditVO salesAuditVO) {
        int i = projectManageService.updateAuditInfo(salesAuditVO);
        return ResponseAPI.successData(i);
    }

    /**
     * 获取stateCode状态的所有机会
     *
     * @param record 机会信息
     * @param token
     * @return 所有匹配的机会点集合
     */
    @PostMapping("/queryOpportunitiesByState/")
    public ResponseAPI fuzzyQueryOpportunity(@RequestBody SalesOpportunityVO record, @RequestHeader("token") String token) {
//        QueryWrapper<SalesOpportunityVO> queryWrapper = QueryWrapperGenerator.opStateQueryWrapper(record);
        return ResponseAPI.successData(salesOpportunityService.selectAllOpInfo(record));
    }

    /**
     * 查询项目中的机会信息
     *
     * @return
     */
    @GetMapping("/queryProjectOfOpportunity/")
    public ResponseAPI queryProjectOfOpportunity() {
        List<Integer> list = projectManageService.queryProjectOfOpportunity();
        return ResponseAPI.successData(list);
    }

    /**
     * 审批通过
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/approveProjectInfoOne")
    public ResponseAPI approveProjectInfoOne(@RequestHeader("token") String token, @RequestBody SalesProjectManageVO projectManageVO) {
        return ResponseAPI.successData(projectManageService.approveProjectInfoOne(projectManageVO, token, false));
    }

    /**
     * 审批驳回
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/rejectionProjectInfoOne")
    public ResponseAPI rejectionProjectInfoOne(@RequestHeader("token") String token, @RequestBody SalesProjectManageVO projectManageVO) {
        return ResponseAPI.successData(projectManageService.rejectionProjectInfoOne(projectManageVO, token));
    }

    /**
     * 审批通过
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/approveProjectInfo")
    public ResponseAPI approveProjectInfo(@RequestHeader("token") String token, @RequestBody SalesProjectManageVO projectManageVO) {
        return ResponseAPI.successData(projectManageService.approveProjectInfo(projectManageVO, token, false));
    }

    /**
     * 审批驳回
     *
     * @param projectManageVO
     * @return
     */
    @PostMapping("/rejectionProjectInfo")
    public ResponseAPI rejectionProjectInfo(@RequestHeader("token") String token, @RequestBody SalesProjectManageVO projectManageVO) {
        return ResponseAPI.successData(projectManageService.rejectionProjectInfo(projectManageVO, token));
    }

    /**
     * 根据工作流ID获取终审信息
     *
     * @param workId
     * @return
     */
    @PostMapping("/queryProjectApprovalByWorkId/{workId}")
    public ResponseAPI queryProjectApprovalByWorkId(@PathVariable("workId") Integer workId) {
        return ResponseAPI.successData(projectManageService.queryProjectApprovalByWorkId(workId));
    }

    /**
     * 根据工作流ID获取初审信息
     *
     * @param workId
     * @return
     */
    @PostMapping("/queryProjectApprovalOneByWorkId/{workId}")
    public ResponseAPI queryProjectApprovalOneByWorkId(@PathVariable("workId") Integer workId,@RequestHeader("token") String token) {
        return ResponseAPI.successData(projectManageService.queryProjectApprovalOneByWorkId(workId,token));
    }

    /**
     * 根据工作流查询历史终审记录
     *
     * @param approvalId
     * @return
     */
    @PostMapping("/queryHistoryProjectApprovalByApprovalId/{approvalId}")
    public ResponseAPI queryHistoryProjectApprovalByApprovalId(@PathVariable("approvalId") Integer approvalId) {
        return ResponseAPI.successData(projectManageService.queryHistoryProjectApprovalByApprovalId(approvalId));
    }

    /**
     * 根据工作流查询历史初审记录
     *
     * @param approvalId
     * @return
     */
    @PostMapping("/queryHistoryProjectApprovalOneByApprovalId/{approvalId}")
    public ResponseAPI queryHistoryProjectApprovalOneByApprovalId(@PathVariable("approvalId") Integer approvalId,@RequestHeader("token") String token) {
        return ResponseAPI.successData(projectManageService.queryHistoryProjectApprovalOneByApprovalId(approvalId,token));
    }

    /**
     * 查询审批项目信息
     *
     * @param pageNum
     * @param pageSize
     * @param projectManageVO
     * @return
     */
    @PostMapping("/queryTodoProjectsByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryTodoProjectsByPage(@PathVariable(value = "pageNum") int pageNum,
                                               @PathVariable(value = "pageSize") int pageSize,
                                               @RequestBody SalesProjectManageVO projectManageVO) {
        IPage<SalesProjectManageVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<SalesProjectManageVO> result = projectManageService.queryTodoProjectsByPage(page, projectManageVO);
        return ResponseAPI.successData(result);
    }

    /**
     * 依据项目ID获取项目信息
     *
     * @param projectID
     * @return
     */
    @PostMapping("/queryProjectByID/{projectID}")
    public ResponseAPI queryProjectByID(@PathVariable(value = "projectID") int projectID) {
        SalesProjectManageVO projectManageVO = projectManageService.queryProjectByID(projectID);
        return ResponseAPI.successData(projectManageVO);
    }

    @PostMapping("/queryProjectByIDFromCache/{projectID}")
    public ResponseAPI queryProjectByIDFromCache(@PathVariable(value = "projectID") int projectID,@RequestHeader("token") String token) {
        SalesProjectManageVO projectManageVO = projectManageService.queryProjectByIDFromCache(projectID,token);
        return ResponseAPI.successData(projectManageVO);
    }

    /**
     * 获取全部项目信息
     *
     * @return
     */
    @PostMapping("/queryProjects")
    public ResponseAPI queryProjects() {
        List<SalesProjectManageVO> projectManages = projectManageService.queryProjects(null);
        return ResponseAPI.successData(projectManages);
    }

    /**
     * 获取初审通过的项目信息
     *
     * @return
     */
    @PostMapping("/queryProjects/{source}")
    public ResponseAPI queryProjects(@PathVariable("source") String source) {
        List<SalesProjectManageVO> projectManages = projectManageService.queryProjects(source);
        return ResponseAPI.successData(projectManages);
    }

    /**
     * 文件上传
     *
     * @param file      文件
     * @param id        机会点ID
     * @param fileClass 文件类型
     * @return 上传的文件记录、用于前端构造fileList
     */
    @PostMapping("/uploadFile/{id}/{fileClass}")
    public ResponseAPI uploadFileForOpportunity(@RequestParam("file") MultipartFile file,
                                                FileUploadRecordVO fileUploadRecord,
                                                @PathVariable("id") Integer id,
                                                @PathVariable("fileClass") Integer fileClass,
                                                @RequestHeader("token") String token) {
        // 调用远程上传服务
        FileUploadRecordVO fileUploadRecords = fileService.upload2Ftp(file, fileUploadRecord, token).getResult();
        // 将结果进行记录
        SalesFileRelationVO salesFileRelationVO = new SalesFileRelationVO();
        salesFileRelationVO.setSalesRelationId(id);
        salesFileRelationVO.setFileClass(fileClass);
        salesFileRelationVO.setCreator(AuthUtils.getUserId());
        salesFileRelationVO.setCreateDataTime(new Timestamp(new Date().getTime()));
        salesFileRelationVO.setFileId(fileUploadRecords.getId());
        salesFileRelationVO.setFileName(fileUploadRecords.getOriFileName());
        salesFileRelationVO.setContentType(fileUploadRecords.getContentType());
        salesFileRelationVO.setFlag(1);
        salesFileRelationService.save(salesFileRelationVO);
        return ResponseAPI.successData(fileUploadRecords);
    }

    /**
     * 导出Excel
     *
     * @param response
     * @param entity
     * @throws Exception
     */
    @PostMapping("/exportProjectInfo")
    public void exportProjectInfo(HttpServletResponse response, @RequestBody SalesProjectManageVO entity, @RequestHeader("token") String token) throws Exception {
//        Integer userId = AuthUtils.getUserId();
//        int authId = projectMemberService.queryAuth(userId,Constants.PROJECT_MODEL_NAME);
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(), "projectManage", token);
        List<SalesProjectManageVO> list = projectManageService.queryProject(entity, userList, token);
        projectManageService.exportProjectInfo(response, list, token);
    }

    /**
     * 根据采购id获取项目信息
     *
     * @return
     */
    @PostMapping("/queryProjectByPurchaseId/{id}")
    public ResponseAPI queryProjectByPurchaseId(@PathVariable(value = "id") Integer id) {
        SalesProjectManageVO projectManageVO = projectManageService.queryProjectByPurchaseId(id);
        return ResponseAPI.successData(projectManageVO);
    }

    /**
     * 更具ID获取项目负责人信息
     *
     * @return
     */
    @PostMapping("/queryProjectHeadByProjectId/{projectId}")
    public ResponseAPI queryProjectHeadByProjectId(@PathVariable(value = "projectId") Integer projectId,@RequestHeader("token") String token) {
        SysUserEntity header = projectManageService.queryProjectHeadByProjectId(projectId,token);
        return ResponseAPI.successData(header);
    }

    /**
     * 转移负责人
     * @param userId
     * @param voList
     * @param token
     * @return
     */
    @PostMapping("/transforPrincipal/{userId}")
    public ResponseAPI transforPrincipal(@PathVariable(value = "userId") Integer userId,@RequestBody List<SalesProjectManageVO> voList,@RequestHeader("token") String token) {
        return ResponseAPI.successData(projectManageService.transforPrincipal(userId,voList,token));
    }

    /**
     * 项目发起的变更提交
     *
     * @param projectId 需更新实体
     * @return 成功失败
     * @Timestamp: 2022-11-22
     * @Author: WuJie
     */
    @PostMapping("/updateManagerSubmit/{projectId}")
    public ResponseAPI updateManagerSubmit(@PathVariable(value = "projectId") Integer projectId,@RequestBody ProjectUpdateInfoVO reasonVO, @RequestHeader("token") String token) {
        ActivitiVO data = projectManageService.updateManagerSubmit(projectId,reasonVO,token);
        return ResponseAPI.successData(data);
    }

    /**
     * 项目发起的变更审批通过
     * @param projectId
     * @param token
     * @return
     */
    @PostMapping("/updateManagerApprove/{projectId}")
    public ResponseAPI updateManagerApprove(@PathVariable(value = "projectId") Integer projectId,@RequestHeader("token") String token) {
        Boolean data = projectManageService.updateManagerApprove(projectId,token,false);
        return ResponseAPI.successData(data);
    }

    /**
     * 项目发起的变更审批驳回
     * @param projectId
     * @param token
     * @return
     */
    @PostMapping("/updateManagerReject/{projectId}")
    public ResponseAPI updateManagerReject(@PathVariable(value = "projectId") Integer projectId,@RequestHeader("token") String token) {
        Boolean data = projectManageService.updateManagerReject(projectId,token);
        return ResponseAPI.successData(data);
    }

    @GetMapping("/queryUpdateInfoByApproveId/{approveId}")
    public ResponseAPI  queryUpdateInfoByApproveId(@PathVariable(value = "approveId") Integer approveId){
        List<ProjectUpdateInfoVO> updateInfoList = projectManageService.queryUpdateInfoByApproveId(approveId);
        return ResponseAPI.successData(updateInfoList);
    }



    @PostMapping("/saveUpdateInfoToDBTemp")
    public ResponseAPI saveUpdateInfoToDBTemp(@RequestBody List<ProjectUpdateInfoVO> projectUpdateInfoVOs,@RequestHeader("token") String token) {
        projectManageService.saveUpdateInfoToDBTemp(projectUpdateInfoVOs,token);
        return ResponseAPI.successData(projectUpdateInfoVOs);
    }




    @PostMapping("/queryPaymentPlanFromCache")
    public ResponseAPI queryPaymentPlanFromCache(@RequestBody List<PurchaseOrderVO> purchaseOrderVOs, @RequestHeader("token") String token) {
        List<PurchaseOrderVO> updateAfterPaymentPlans = projectManageService.queryPaymentPlanFromCache(purchaseOrderVOs,token);
        return ResponseAPI.successData(updateAfterPaymentPlans);
    }



    @GetMapping("/queryUpdateInfoById/{projectManageId}")
    public ResponseAPI  queryUpdateInfoById(@PathVariable(value = "projectManageId") Integer projectManageId){
        List<ProjectUpdateInfoVO> updateInfoList = projectManageService.queryUpdateInfoById(projectManageId);
        return ResponseAPI.successData(updateInfoList);
    }

    @GetMapping("/queryUpdateInfoOnLineById/{projectManageId}")
    public ResponseAPI  queryUpdateInfoOnLineById(@PathVariable(value = "projectManageId") Integer projectManageId){
        List<ProjectUpdateInfoVO> updateInfoList = projectManageService.queryUpdateInfoOnLineById(projectManageId);
        return ResponseAPI.successData(updateInfoList);
    }

    @PostMapping("/updateFinalBusinessToDB/{projectId}")
    public ResponseAPI updateFinalBusinessToDB(@PathVariable(value = "projectId") Integer projectId,@RequestHeader("token") String token) {
        projectManageService.updateFinalBusinessToDB(projectId,token);
        return ResponseAPI.successData(null);
    }


    /**
     * 通过文件关系类型、关系主键查询文件
     */
    @PostMapping("/queryFileByRelationIdAndFileClass/{salesRelationId}/{fileClass}")
    public ResponseAPI queryFileByRelationIdAndFileClass(@PathVariable(value = "salesRelationId") Integer salesRelationId
            ,@PathVariable(value = "fileClass") Integer fileClass){
        return ResponseAPI.successData(salesFileRelationService.queryFileByRelationIdAndFileClass(salesRelationId,fileClass));
    }


    @PostMapping("/updateStatusAfterSubmit/{businessId}/{projectId}")
    public ResponseAPI updateStatusAfterSubmit(@PathVariable(value = "businessId") Integer businessId,@PathVariable(value = "projectId") Integer projectId,@RequestHeader("token") String token) {
        projectManageService.updateStatusAfterSubmit(businessId,projectId,token);
        return ResponseAPI.successData("success");
    }


    /**
     * 清空本次修改内容
     *
     * @param projectId
     * @param token
     * @return
     */
    @PostMapping("/deleteUpdateInfoByProjectId/{projectId}")
    public ResponseAPI deleteUpdateInfoByProjectId(@PathVariable(value = "projectId") Integer projectId ,@RequestHeader("token") String token){
        projectManageService.deleteUpdateInfoByProjectId(projectId,token);
        return ResponseAPI.successData("success");
    }

    /**
     * 清空本次修改内容
     *
     * @param projectId
     * @param token
     * @return
     */
    @PostMapping("/queryApproveIdByManagerId/{projectId}")
    public ResponseAPI queryApproveIdByManagerId(@PathVariable(value = "projectId") Integer projectId ,@RequestHeader("token") String token){
        Integer approveId  = projectManageService.queryApproveIdByManagerId(projectId,token);
        return ResponseAPI.successData(approveId);
    }

    /**
     * 根据关键字模糊搜索变更内容
     *
     * @param projectUpdateInfoVO
     * @param token
     * @return
     */
    @PostMapping("/queryUpdateInfoListBySearch")
    public ResponseAPI queryUpdateInfoListBySearch(@RequestBody ProjectUpdateInfoVO  projectUpdateInfoVO, @RequestHeader("token") String token) {
        List<ProjectUpdateInfoVO> projectUpdateInfoList = projectManageService.queryUpdateInfoListBySearch(projectUpdateInfoVO,token);
        return ResponseAPI.successData(projectUpdateInfoList);
    }

    /**
     * 检查是否有审批中的对象
     * @param projectManageVO
     * @param token
     * @return
     */
    @PostMapping("/testFinalReview")
    public ResponseAPI testFinalReview(@RequestBody SalesProjectManageVO  projectManageVO, @RequestHeader("token") String token) {
        return ResponseAPI.successData(projectMemberService.testFinalReview(projectManageVO,token));
    }

    /**
     * 项目终验变初验的处理
     * true表示通过，已更改状态信息；false代表失败
     * @param projectManageVO
     * @param token
     * @return
     */
    @PostMapping("/projectStateUpdate")
    public ResponseAPI projectStateUpdate(@RequestBody SalesProjectManageVO  projectManageVO, @RequestHeader("token") String token) {
        return ResponseAPI.successData(projectManageService.projectStateUpdate(projectManageVO,token));
    }

    /**
     * 获取全部项目信息用于筛选
     * @return
     */
    @PostMapping("/queryAllProjectsByUser")
    public ResponseAPI queryAllProjectsByUser(@RequestHeader("token") String token) {
        return ResponseAPI.successData(projectManageService.queryAllProjectsByUser(token));
    }

    /**
     * 获取全部项目信息用于筛选
     * @return
     */
    @PostMapping("/queryAllProjects")
    public ResponseAPI queryAllProjects(@RequestHeader("token") String token) {
        return ResponseAPI.successData(projectManageService.queryAllProjects());
    }

}
